// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import axios from './http'
import {appRouter,R} from './router'
import Router from 'vue-router'
import iView from 'iview';
import Vuex from 'vuex';
import Cookies from 'js-cookie';
import store from './store';
import * as types from './store/mutation-types';

import 'iview/dist/styles/iview.css';

Vue.use(Router)
Vue.use(iView);
Vue.use(Vuex);


Vue.config.productionTip = false
// 将axios挂载到prototype上，在组件中可以直接使用this.axios访问
Vue.prototype.axios = axios;

// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: R
};

const router = new Router(RouterConfig);

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.token) {
      console.info(store.state.token);
      next();
    }
    else {
      next({
        name: 'login'
      })
    }
  }
  else {
    next();
  }
});

router.afterEach(() => {
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});
// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
  store.commit(types.LOGIN, window.localStorage.getItem('token'))
}
/* eslint-disable no-new */
new Vue({
  axios,
  router,
  store: store,
  created () {
    let tagsList = [];
    appRouter.map((item) => {
      if (item.children.length <= 1) {
        tagsList.push(item.children[0]);
      } else {
        tagsList.push(...item.children);
      }
    });
    this.$store.commit('setTagsList', tagsList);
    this.$store.commit('updateMenulist', appRouter);
  }
}).$mount('#app')
